package com.novacloud.fresh.controller.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Validator;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2016/1/17 0017.
 */


public class UserValidator implements Validator {
    @Override
    public boolean supports(Class<?> aClass) {
        return false;
    }

    @Override
    public void validate(Object o, Errors errors) {

    }

//
//
//    @Autowired
//    private UserService userService;
//
//    @Override
//    public boolean supports(Class<?> aClass) {
//        return User.class.equals(aClass);
//    }
//
//    @Override
//    public void validate(Object obj, Errors errors) {
//        User user = (User) obj;
//        if (null == user.getUsername() || "".equals(user.getUsername())) {
//            errors.rejectValue("username", "screen.register.errors.username");
//        } else if (userService.getByUserName(user.getUsername()) != null) {
//            errors.rejectValue("username", "screen.register.errors.username.exits");
//        }
//        if (null == user.getPassword() || "".equals(user.getPassword())) {
//            errors.rejectValue("password", "screen.register.errors.password");
//        }
//        if (null == user.getEmail() || "".equals(user.getEmail())) {
//            errors.rejectValue("email", "screen.register.errors.email");
//        } else {
//            String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
//            Pattern regex = Pattern.compile(check);
//            Matcher matcher = regex.matcher(user.getEmail());
//            if (matcher.matches()) {
//                User usermail = userService.getByEmail(user.getEmail());
//                if (null != usermail || !"".equals(usermail)) {
//                    errors.rejectValue("email", "screen.register.errors.emial.exits");
//                }
//            } else {
//                errors.rejectValue("email", "screen.register.label.email.check");
//            }
//        }
//    }
}
